﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Itop.YHPC.Model;
using System.Collections.Generic;
using Itop.YHPC.controller;

namespace Itop.YHPC.workflow
{
    public partial class workflow_init_ajax : AjaxPage<yhpcMain>
    {
        protected override IList<yhpcMain> onRead()
        {
            string wfid = Request.QueryString["wfid"];
            IList<yhpcMain> list = new List<yhpcMain>();
            yhpcMain main = new yhpcMain();
            main.yhjt = "长春供电公司" + DateTime.Now.ToString("yyyy年M月d日") + "，发现";
            main.yhbh = GetSecOrg() + DateTime.Now.Year.ToString() + GetMaxNumber().ToString("0000");
            main.yhdw = GetSecOrg();
            main.yhfxr = (Session["user"] as sysuser).UserName;
            main.fxrdw = GetSecOrg();
            main.yssfxc = "否";
            main.lcflag = wfid;
            main.fxrq = DateTime.Now;
            list.Add(main);
            return list;

        }

        public int GetMaxNumber()
        {
            return workflowController.GetMainRowCount("where id like '"+DateTime.Now.Year.ToString()+"%'")+1;
        }

        public string GetSecOrg()
        {
            string rst = "长春供电公司";
            IList<yhpcOrg> list = yhpcOrgController.GetList(" where id = (select dwid from sysuser where LoginID='" + (Session["user"] as sysuser).LoginID + "')");
            yhpcOrg org = null;
            if (list.Count > 0)
            {
                org = list[0];
                if (list[0].dwType != "二级单位")
                    org = FindSecOrg(list[0]);
            }
            return org == null ? rst : org.dwName;
        }

        public yhpcOrg FindSecOrg(yhpcOrg org)
        {
            yhpcOrg rst = null;
            rst = yhpcOrgController.Bll.Get(org.ParentID);
            if (rst != null && rst.dwType != "二级单位")
                FindSecOrg(rst);
            return rst;
        }
    }
}
